package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.mapper.DestinationMapper;
import cn.wolfcode.luowowo.article.query.DestinationQueryObject;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by admin on 2019/8/9.
 */

@Service
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationMapper destinationMapper;

    // 查询出关联目的地
    @Override
    public List<Destination> queryForList(int deep) {
        return destinationMapper.selectAll(deep);
    }

    // 查询目的地列表
    @Override
    public PageInfo query(DestinationQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Destination> list = destinationMapper.selectForList(qo);
        return new PageInfo(list);
    }

    // 查询出地区层级
    @Override
    public List<Destination> queryForParent(Long id) {
        List<Destination> list = new ArrayList<>();
        //根>中国>广东>广州
        //parentId = 广州id
        //广州
        //广州.getParentId() 广东
        //广东.getParentId() 中国
        //根
        createToasts(id, list);
        //翻转
        Collections.reverse(list);
        return list;
    }

    private void createToasts(Long parentId, List<Destination> list) {
        Destination destination = destinationMapper.selectByPrimaryKey(parentId);
        if (destination == null) {
            return;
        }
        //广州
        list.add(destination);
        if (destination.getParent() != null) {
            createToasts(destination.getParent().getId(), list);
        }
    }

    // 修改目的地热门状态
    @Override
    public void changeHotValueById(int hot, Long id) {
        destinationMapper.updateHotValueById(hot, id);
    }

    // 删除目的地
    @Override
    public void deleteDestinationById(Long id) {
        destinationMapper.deleteByPrimaryKey(id);
    }

    // 修改目的地内容
    @Override
    public void setInfo(Long id, String info) {
        destinationMapper.updateInfoById(id, info);
    }

    // 获取国家数据
    @Override
    public Destination getCountry(Long id) {
        List<Destination> destinations = this.queryForParent(id);
        if (destinations != null && destinations.size() > 0) {
            Destination destination = destinations.get(0);
            if (destination.getId() == 1) {
                return null;
            }
            return destination;
        }
        return null;
    }

    // 获取省份数据
    @Override
    public Destination getProvince(Long id) {
        List<Destination> destinations = this.queryForParent(id);
        if (destinations != null && destinations.size() > 0) {
            if (destinations.get(0).getId() != 1) {
                return null;
            }
        }
        if (destinations.size() > 1) {
            return destinations.get(1);
        }
        if (destinations.size() == 1) {
            return destinations.get(0);
        }
        return null;
    }

    @Override
    public List<Destination> list() {
        return destinationMapper.selectList();
    }

    public Destination getByUserId(Long id) {
        return destinationMapper.getByUserId(id);
    }

    public List<Destination> selectDestByUserId(Long id) {
        return destinationMapper.selectByUserId(id);
    }


}
